package com.zzty.zzlp.serviceimpl;


import com.zzty.zzlp.dao.TeamExamMapper;
import com.zzty.zzlp.dao.UserDao;
import com.zzty.zzlp.entity.TeamExam;
import com.zzty.zzlp.entity.User;
import com.zzty.zzlp.service.TeamExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class TeamExamServiceImpl implements TeamExamService {

    @Autowired
    TeamExamMapper teamExamMapper;

    @Autowired
    UserDao userDao;

    public String[] getQuestionID(String date, Integer clazzId) {
        TeamExam te = teamExamMapper.getInfoByDateAndClazzId(date, clazzId);
        String questions = te.getQuestions();
        String[] array = questions.split("\\|");
        return array;
    }

    public Integer searchExamId(String date, Integer clazzId) {
        TeamExam te = teamExamMapper.getInfoByDateAndClazzId(date, clazzId);
        if (te == null) {
            return null;
        }
        Integer examId = te.getId();
        return examId;
    }

    public Integer searchClazzId(String name) {
        User user1 = userDao.selectUserByName(name);
        return user1.getClazzId();
    }

    public void addTeamExam(TeamExam te) {
        Integer id = teamExamMapper.getNextId();
        te.setId(id);
        teamExamMapper.addTeamExam(te);
    }

    public void updateWinners(Integer examId, Integer winnerId1, Integer winnerId2) {
        TeamExam te = new TeamExam();
        te.setId(examId);
        te.setWinnerId1(winnerId1);
        te.setWinnerId2(winnerId2);
        teamExamMapper.updateWinnerIds(te);
    }

    public List<TeamExam> searchTeamExamByClazzId(Integer clazzId, Integer page) {
        return teamExamMapper.searchTeamExamByClazzId(clazzId, page);
    }

    @Override
    public List<TeamExam> searchTeamExamByClazzId(Integer clazzId) {
        return teamExamMapper.searchTeamExamOnlyByClazzId(clazzId);
    }

    public Integer searchNewId(String date, String questions, int clazzId) {
        return teamExamMapper.searchNewId(date, questions, clazzId);
    }

    @Override
    public List<TeamExam> searchTeamExamByWinnerIdAndClazzId(Integer clazzId) {

        return teamExamMapper.searchTeamExamByWinnerIdAndClazzId(clazzId);
    }

    @Override
    public List<TeamExam> searchTenTeamExamByClazzId(Integer clazzId, Integer page) {

        return teamExamMapper.searchTenTeamExamByClazzId(clazzId, page);
    }


}
